﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Gateway.Interfaces;
using Phoenix.RedisConnectors;
using PServiceBus.Core.Runtime;

namespace PServiceBus.Gateway.Redis {
    public class RedisGatewayClient : IGatewayClient {
        private RedisSender _client;

        public RedisGatewayClient() { }

        public RedisGatewayClient(string address) {
            var endpoint = EndpointUri.Parse(address);
            _client = new RedisSender(RedisConstants.RPCServiceName, endpoint.Host, endpoint.Port);
        }

        #region IGatewayClient Members

        public void Send<TCommand>(TCommand command) where TCommand : class {
            _client.Send<TCommand>(command);
        }

        public TResponse Send<TRequest, TResponse>(TRequest request)
            where TRequest : class
            where TResponse : class {
            return _client.Send<TRequest, TResponse>(request);
        }

        public IGatewayClient Init(string address) {
            return new RedisGatewayClient(address);
        }

        #endregion

        #region IDisposable Members

        public void Dispose() {
            if (_client != null) _client.Dispose();
            _client = null;
        }

        #endregion
    }
}
